package com.example.login;

import com.example.domain.User;
import com.example.utils.Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author mucd
 * 2021年06月21日  10:00
 */

@WebServlet(value = "/Login")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        String user = req.getParameter("user");
        String password = req.getParameter("password");
        String vcode1 = req.getSession().getAttribute("vcode")+"";

        try {
            boolean b = emptyUser(user, password,vcode1,req,resp);
            //登陆成功 并设置session
            if (b){
                req.getSession().setAttribute("user",user);
                //转发request
                req.getSession().setAttribute("request",req);
                resp.sendRedirect("MySession");
            }else {
                req.getSession().setAttribute("text","账号或密码错误,重新<a href='index.jsp'>登录</a>");
                req.getRequestDispatcher("index.jsp").forward(req,resp);
            }
        } catch (SQLException e) {
            req.getRequestDispatcher("index.jsp").forward(req,resp);

            e.printStackTrace();
        }
    }

    /**
     * 判断用户是否存在
     */
    private boolean emptyUser(String user ,String password,String code,HttpServletRequest request,HttpServletResponse response) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(Utils.getDataSource());
        List<User> query = queryRunner.query(
                "select * from web.username"
                , new BeanListHandler<>(User.class)
        );

        HttpSession session = request.getSession();
        if (query != null){
            String vcode = (String)session.getAttribute("vcode");
            if (code.equals(vcode)){
                for (User u : query){
                    boolean equalsUser = u.getUser().equals(user);
                    boolean equalsPasswd = u.getPassword().equals(password);
                    if (equalsPasswd && equalsUser){
                        return true;
                    }
                }
            }else {
                session.setAttribute("login","验证码错误");
                try {
                    response.setStatus(302);
                    response.sendRedirect("index.jsp");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return false;
    }
}